package listbyorder.access101_200.test200;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/15 14:42
 */
public class Solution1 {

    int count = 0;

    public int numIslands(char[][] grid) {
        if(grid.length == 0 || grid[0].length == 0) return 0;
        int r = grid.length;
        int c = grid[0].length;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    infect(grid, i, j, r, c);
                }
            }
        }
        return count;
    }

    private void infect(char[][] grid, int i, int j, int r, int c) {
        if (i < 0 || j < 0 || i == r || j == c) return;
        if (grid[i][j] != '1') return;
        grid[i][j] = '2';
        infect(grid, i + 1, j, r, c);
        infect(grid, i - 1, j, r, c);
        infect(grid, i, j + 1, r, c);
        infect(grid, i, j - 1, r, c);
    }
}
